package com.woyou.utils;

import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;

import com.woyou.MyApplication;
import com.woyou.ui.activity.MyInfoActivity.ImageCallback;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

public class ImageUtils {
	
	public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {  
		 
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);  
 
        Canvas canvas = new Canvas(output);  
 
        final int color = 0xff424242;  
 
        final Paint paint = new Paint();  
 
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());  
 
        final RectF rectF = new RectF(rect);  
 
        final float roundPx = pixels;  
 
        paint.setAntiAlias(true);  
 
        canvas.drawARGB(0, 0, 0, 0);  
 
        paint.setColor(color);  
 
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
 
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
 
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;  
    }

	public Drawable getDrawableFromCache(String url){
		if(TextUtils.isEmpty(url)){
			return null;
		}
		SoftReference<Drawable> softReference = imageCache.get(url);
		if(softReference != null){
			return softReference.get();
		}
		return null;
	}
	
	private static HashMap<String, SoftReference<Drawable>> imageCache = new HashMap<String, SoftReference<Drawable>>();

	private static final String TAG = "AsyncImageLoader";
	private Handler handler;

	public ImageUtils() {
		handler = new Handler();
	}
	
	public Drawable getDrawable(String imageUrl){
		SoftReference<Drawable> softReference = imageCache.get(imageUrl);
		if(softReference!= null){
			return softReference.get();
		}else{
			return null;
		}
	}
	
	public void putDrawable(String imageUrl,Drawable drawable){
		if(drawable == null){
			return;
		}
		imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
	}

	public Drawable loadDrawable(final String imageUrl,final ImageCallback imageCallback) {

		if (imageCache.containsKey(imageUrl)) {
			SoftReference<Drawable> softReference = imageCache.get(imageUrl);
			Drawable drawable = softReference.get();

			if (drawable != null) {
				return drawable;
			}
		}

		new Thread() {

			@Override
			public void run() {

				final Drawable drawable = loadImageFromUrl(imageUrl);
				if(drawable != null){
					imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
				}
				Runnable r = new Runnable(){

					public void run() {
						// TODO Auto-generated method stub
						imageCallback.imageLoaded(drawable, imageUrl);
					}
				};
				handler.post(r);
			}
		}.start();
		return null;
	}

	@SuppressWarnings("deprecation")
	public Drawable loadImageFromUrl(String url) { 
		URL m;
		InputStream i = null;

		try {
			m = new URL(url);
			i = (InputStream) m.getContent();
			Log.d(TAG, "i.available()  "+i.available());
			if(i.available()> 1024*1024){
				Log.e(TAG, "image size = "+ i.available()+" , too large for android");
				return null;
			}
			BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
			if(i.available() > 100*1024){
				bitmapOptions.inSampleSize = 4;
			}else if(i.available() > 10*1024){
				bitmapOptions.inSampleSize = 2;
			}
			
			bitmapOptions.inPreferredConfig = Config.RGB_565;
			Bitmap bitmap = BitmapFactory.decodeStream(i, null , bitmapOptions);
			BitmapDrawable d = new BitmapDrawable(bitmap);
			System.gc();
			return d;
		} catch (Exception e) {
			if(MyApplication.isShowLog)Log.e("AsyncImageLoader", " url : "+url,e);
		}finally{
			if(i != null){
				try {
					i.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return null;
	}

//	public interface ImageCallback {
//		public void imageLoaded(Drawable imageDrawable, String imageUrl);
//	}
}
